Skip to content

watch: track worker thread entry files in --watch mode#62368

Open
SudhansuBandha wants to merge 6 commits intonodejs:mainfrom
SudhansuBandha:watch-worker-support
Open

watch: track worker thread entry files in --watch mode#62368
SudhansuBandha wants to merge 6 commits intonodejs:mainfrom
SudhansuBandha:watch-worker-support

Conversation

@SudhansuBandha
Copy link
Copy Markdown

Currently, --watch mode only tracks dependencies from the main module graph (require/import). Worker thread entry points created via new Worker() are not included, so changes to worker files do not trigger restarts.

This change hooks into Worker initialization and registers the worker entry file with the watch mode, ensuring restarts when worker files change.

Fixes: #62275

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support. labels Mar 21, 2026
@SudhansuBandha SudhansuBandha marked this pull request as ready for review March 21, 2026 17:55
@clemyan
Copy link
Copy Markdown

clemyan commented Mar 22, 2026

This solves the issue for the worker module itself, but not its dependencies. I.e. if a worker module imports/requires another module, modifying the latter still does not trigger re-run.

@SudhansuBandha
Copy link
Copy Markdown
Author

I have updated changes to watch dependencies for worker as well, so changes in imported/required modules will now trigger a re-run.
@clemyan

@clemyan
Copy link
Copy Markdown

clemyan commented Mar 23, 2026

This is the wrong way to solve this, for a myriad of reasons: (based on a cursory glance of the code. Did not bother to actual test it)

  1. It is impossible to properly parse JS with regex. This approach cannot even distinguish if the "import" is part of a string or a comment
  2. It only works for direct dependencies of the worker module, not transitively. (I.e. it does not watch dependencies of dependencies, and their dependencies, etc.)
  3. It only works for literals specifiers that are relative paths, not for absolute paths require('/path/to/file.js'), packages require('my-pkg/sub/path'), dynamic specifier require('./messages/'+language+'/string.json'), or paths that are resolved by customization hooks
  4. What about the worker module itself? If it is loaded by customization hooks, there isn't necessarily a file on disk to readSync from.

Any solution must be integrated into Node's loading pipeline

@SudhansuBandha
Copy link
Copy Markdown
Author

SudhansuBandha commented Mar 23, 2026

I will revert back these updates and work on the suggestions
@clemyan

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submit the change.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@SudhansuBandha
Copy link
Copy Markdown
Author

SudhansuBandha commented Mar 26, 2026

@clemyan
I have updated handling of dependencies from worker thread. For this I have also reverted my changes from the initial commit as well f9beab0
I have implemented the solution for both cjs and esm files.
Do let me know your feedback on the proposed solution and whether it makes sense to proceed further in this direction.

@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Mar 30, 2026

The added test already passes on main, that means either the code change is not necessary, or that it's not actually testing what you're trying to achieve

@SudhansuBandha
Copy link
Copy Markdown
Author

Sure!! @aduh95 I will update the tests for this. Thanks for your feedback!!

@SudhansuBandha SudhansuBandha force-pushed the watch-worker-support branch 2 times, most recently from d4e5305 to 634a756 Compare March 31, 2026 09:42
@SudhansuBandha
Copy link
Copy Markdown
Author

@aduh95 Please do review the updated tests for the proposed changes based on behaviour of worker file and it's dependencies in --watch mode for both cjs and esm modules.

@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Apr 1, 2026

Can you not put the tests in test/sequential/test-watch-mode.mjs? That file is enormous, which is becoming a maintenance issue/

@SudhansuBandha
Copy link
Copy Markdown
Author

SudhansuBandha commented Apr 2, 2026

@aduh95 As suggested I have removed tests from test/sequential/test-watch-mode.mjs to a new file and made few lint fixes.
Do let me know if any further changes are required

@SudhansuBandha SudhansuBandha requested a review from aduh95 April 2, 2026 09:45
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 4, 2026

Codecov Report

❌ Patch coverage is 93.61702% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.71%. Comparing base (4b3d82c) to head (f7a05a3).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/cjs/loader.js 87.50% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #62368   +/-   ##
=======================================
  Coverage   89.70%   89.71%           
=======================================
  Files         695      695           
  Lines      214476   214523   +47     
  Branches    41072    41075    +3     
=======================================
+ Hits       192405   192464   +59     
+ Misses      14115    14109    -6     
+ Partials     7956     7950    -6     
Files with missing lines Coverage Δ
lib/internal/modules/esm/loader.js 98.79% <100.00%> (+0.01%) ⬆️
lib/internal/worker.js 96.58% <100.00%> (+0.06%) ⬆️
lib/internal/modules/cjs/loader.js 98.25% <87.50%> (+0.11%) ⬆️

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Currently, --watch mode only tracks dependencies from the main
module graph (require/import). Worker thread entry points created
via new Worker() are not included, so changes to worker files do
not trigger restarts.

This change hooks into Worker initialization and registers the
worker entry file with watch mode, ensuring restarts when worker
files change.

Fixes: nodejs#62275
Signed-off-by: SudhansuBandha <sudhansu9.vssut@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Watch mode not triggered by worker module and dependencies

5 participants